package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.AgedmallCustomGoodsMapper;
import org.linlinjava.litemall.db.dao.AgedmallDeductionRuleMapper;
import org.linlinjava.litemall.db.dao.AgedmallGoodsDeductionConfMapper;
import org.linlinjava.litemall.db.dao.AgedmallStoreMapper;
import org.linlinjava.litemall.db.dao.app.AppDeductionConfMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.DeductionConfConstant;
import org.linlinjava.litemall.db.util.GoodsConstant;
import org.linlinjava.litemall.db.vo.app.StoreGoodsVo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AgedmallGoodsDeductionConfService {

    @Resource
    private AgedmallGoodsDeductionConfMapper agedmallGoodsDeductionConfMapper;
    @Resource
    private AppDeductionConfMapper appDeductionConfMapper;
    @Resource
    private AgedmallStoreMapper agedmallStoreMapper;

    /**
     * 根据商品id获取商品抵扣金配置
     * @param goodsId
     * @return
     */
    public AgedmallGoodsDeductionConf findDeductionByGoodsId(Integer goodsId) {
        AgedmallGoodsDeductionConfExample deductionConfExample = new AgedmallGoodsDeductionConfExample();
        deductionConfExample.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return agedmallGoodsDeductionConfMapper.selectOneByExample(deductionConfExample);
    }

    public List<AgedmallGoodsDeductionConf> findDeductionListByGoodsIds(List<Integer> goodsIdList) {
        if(goodsIdList == null || goodsIdList.size() == 0) {
            return new ArrayList<>();
        }
        AgedmallGoodsDeductionConfExample deductionConfExample = new AgedmallGoodsDeductionConfExample();
        deductionConfExample.or().andGoodsIdIn(goodsIdList).andDeletedEqualTo(false);
        return agedmallGoodsDeductionConfMapper.selectByExample(deductionConfExample);
    }

    public BigDecimal getDeductNumByGoods(LitemallGoods goods) {
        // 抵扣金 只有售卖有抵扣金
        if (GoodsConstant.GOODS_TYPE_SALE.equals(goods.getGoodsType())) {
            AgedmallGoodsDeductionConf deductionConf = findDeductionByGoodsId(goods.getId());
            if (null != deductionConf) {
                // 固定金额抵扣金
                if (DeductionConfConstant.DED_TYPE_AMOUNT.equals(deductionConf.getDedType())) {
                    return deductionConf.getDedCoin();
                }
                // 百分比抵扣金 百分比乘以商品零售价
                if (DeductionConfConstant.DED_TYPE_PERCENT.equals(deductionConf.getDedType())) {
                    return goods.getCounterPrice().multiply(deductionConf.getDedCoin().divide(new BigDecimal(100)));
                }
            }
        }
        return null;
    }

    public BigDecimal getDeductNumByGoodsByMap(LitemallGoods goods, Map<Integer, List<AgedmallGoodsDeductionConf>> goodsDeductionConfMap) {
        // 抵扣金 只有售卖有抵扣金
        if (GoodsConstant.GOODS_TYPE_SALE.equals(goods.getGoodsType())) {
            List<AgedmallGoodsDeductionConf> list = goodsDeductionConfMap.get(goods.getId());
            AgedmallGoodsDeductionConf deductionConf = list != null && list.size() > 0 ? list.get(0) : null;
            if (null != deductionConf) {
                // 固定金额抵扣金
                if (DeductionConfConstant.DED_TYPE_AMOUNT.equals(deductionConf.getDedType())) {
                    return deductionConf.getDedCoin();
                }
                // 百分比抵扣金 百分比乘以商品零售价
                if (DeductionConfConstant.DED_TYPE_PERCENT.equals(deductionConf.getDedType())) {
                    return goods.getCounterPrice().multiply(deductionConf.getDedCoin().divide(new BigDecimal(100)));
                }
            }
        }
        return null;
    }

    public BigDecimal getDeductNumByGoods(StoreGoodsVo storeGoods, Integer storeBusiId) {
        // 抵扣金 只有售卖有抵扣金
        if (GoodsConstant.GOODS_TYPE_SALE.equals(storeGoods.getGoodsType())) {
            AgedmallDeductionRule agedmallDeductionRule = findDeductionRuleByGoodsId(storeBusiId, storeGoods.getStoreId(), storeGoods.getStoreGoodsId());
            if (null != agedmallDeductionRule) {
                // 固定金额抵扣金
                if (DeductionConfConstant.DED_TYPE_AMOUNT.equals(agedmallDeductionRule.getMoneyType())) {
                    return agedmallDeductionRule.getMoney();
                }
                // 百分比抵扣金 百分比乘以商品零售价
                if (DeductionConfConstant.DED_TYPE_PERCENT.equals(agedmallDeductionRule.getMoneyType())) {
                    return storeGoods.getSaleRetailPrice().multiply(agedmallDeductionRule.getMoney().divide(new BigDecimal(100)));
                }
            }
        }
        return null;
    }

    /**
     * 根据商品id获取商品抵扣金配置
     * @return
     */
    public AgedmallDeductionRule findDeductionRuleByGoodsId(Integer busiId, Integer storeId, Integer goodsId) {
        return appDeductionConfMapper.getByStoreAndGoods(busiId, storeId, goodsId);
    }

}
